package cn.edu.cdtu.healthserver.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cdtu.healthserver.domain.RegisterJson;
import cn.edu.cdtu.healthserver.domain.UserInformation;
import cn.edu.cdtu.healthserver.service.UserService;
import cn.edu.cdtu.healthserver.service.impl.UserServiceImplementation;
import cn.edu.cdtu.healthserver.utils.ConnectUtil;

import com.google.gson.Gson;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private UserService userService=new UserServiceImplementation();
    private Gson gson = new Gson();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		//获取数据
		OutputStream writer = response.getOutputStream();
		UserInformation newUser = new UserInformation(
				request.getParameter(ConnectUtil.RegisterPargrams.Pargram.REGISTER_ACCOUNT), 
				request.getParameter(ConnectUtil.RegisterPargrams.Pargram.REGISTER_PASSWORD), 
				request.getParameter(ConnectUtil.RegisterPargrams.Pargram.REGISTER_NAME));
		RegisterJson registerJson = new RegisterJson(userService.addUser(newUser));
		writer.write(gson.toJson(registerJson).getBytes());
		writer.flush();
		writer.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
